package com.apps.ftapp.act;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.activity.BaseActivity;
import com.apps.ftapp.model.BankModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

/**
 * 设置银行账号
 */
public class BankActivity extends BaseActivity {


    @BindView(R.id.name_input)
    EditText nameInput;
    @BindView(R.id.mobile_input)
    EditText mobileInput;
    @BindView(R.id.bank_input)
    EditText bankInput;
    @BindView(R.id.bank_num_input)
    EditText bankNumInput;
    @BindView(R.id.tixian)
    Button tixian;

    private boolean isFlag;

    private Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank);
        ButterKnife.bind(this);

        setTitle("设置银行账号");


        loadData();

        tixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = nameInput.getText().toString();
                String mobile = mobileInput.getText().toString();
                String bank = bankInput.getText().toString();
                String bankNum = bankNumInput.getText().toString();


                if (name == null || mobile == null || bank == null || bankNum == null) {

                    Toasty.normal(getApplicationContext(), "请认真填写信息~").show();
                    return;
                }

                submit(name, mobile, bank, bankNum);

            }
        });

    }


    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("userId", Constant.uid);
        params.put("token", Constant.token);
        Async.post("course/share/account/info", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody) + "......");
                    BankModel result = gson.fromJson(new String(responseBody), BankModel.class);
                    if (Tools.isIntNull(result.getSuccess()) == 1) {

                        nameInput.setText(result.getAccountinfo().getName());
                        mobileInput.setText(result.getAccountinfo().getMobile());
                        bankInput.setText(result.getAccountinfo().getBankname());
                        bankNumInput.setText(result.getAccountinfo().getBankaccount());
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }


    private void submit(String name, String mobile, String bank, String bankNum) {

        RequestParams params = new RequestParams();
        params.put("userId", Constant.uid);
        params.put("token", Constant.token);
        params.put("name", name);
        params.put("mobile", mobile);
        params.put("bankname", bank);
        params.put("bankaccount", bankNum);
        Async.post("course/share/account/add", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody) + "......");
                    BankModel result = gson.fromJson(new String(responseBody), BankModel.class);
                    if (Tools.isIntNull(result.getSuccess()) == 1) {
                        Toasty.normal(getApplicationContext(), "保存成功").show();
                    } else {
                        Toasty.normal(getApplicationContext(), "保存失败").show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }


}
